//
//  RYDizhiCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2020/12/31.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit

class RYDizhiCell: SYBaseCell {

    
    var model: RYAddressListModel!
    
    fileprivate var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5.0.autoWidth()
        return v
    }()
    
    let shouhuorenLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        return v
    }()
    
    let dizhiLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor =  UIColor(hex: 0x666666)
        v.font = 14.autoFontSize()
        v.numberOfLines = 0
        return v
    }()
    
    fileprivate lazy var morenBtn: RYTitleImgView = {
        let v: RYTitleImgView = RYTitleImgView(frame: CGRect.zero)
        v.textLb.text = "设为默认"
        v.imageV.image = UIImage(named: "xuanze")
        v.tapClosure = {[weak self] (index, isSelect) in
            if let `self` = self {
                if let currentvc = (self.currentVC as? RYDizhiViewController) {
                    var dic: [String: String] = ["provinceName": self.model.provinceName ?? "",
                                                 "cityName": self.model.cityName ?? "",
                                                 "districtName": self.model.districtName ?? "",
                                                 "streetName":self.model.streetName ?? "",
                                                 "provinceId": self.model.provinceId ?? "",
                                                 "cityId": self.model.cityId ?? "",
                                                 "districtId": self.model.districtId ?? "",
                                                 "streetId": self.model.streetId ?? "",
                                                 "address": self.model.address ?? "",
                                                 "appUserId": "\(self.model.appUserId ?? 0)",
                                                 "name": self.model.name ?? "",
                                                 "mobile": self.model.mobile ?? "",
                                                 "id": self.model.id ?? "" 
                    ]
                    if isSelect {
                        dic["isDefault"] = "1"
                    } else {
                        dic["isDefault"] = "0"
                    }
                    currentvc.editData(dic)
                }
            }
        }
        return v
    }()
    
    lazy var bianjiBtn: VGBaseTitleImgView = {
        let v: VGBaseTitleImgView = VGBaseTitleImgView(frame: CGRect.zero)
        v.textLb.text = "编辑"
        v.imageV.image = UIImage(named: "bianji")
        v.tapClosure = {[weak self] (index, isSelect) in
            if let weakSelf = self {
                if let currentvc = (weakSelf.currentVC as? RYDizhiViewController) {
                    
                    let vc = RYAddDizhiViewController(type: .edit, model: weakSelf.model)
                    currentvc.sy_push(vc)
                    vc.backClosure = {[weak self] index in
                        if let `self` = self {
                            currentvc.getList()
                        }
                    }
                }
            }
        }
        return v
    }()
    
    fileprivate lazy var shanchuBtn: VGBaseTitleImgView = {
        let v: VGBaseTitleImgView = VGBaseTitleImgView(frame: CGRect.zero)
        v.textLb.text = "删除"
        v.imageV.image = UIImage(named: "shanchu")
        v.tapClosure = {[weak self] (index, isSelect) in
            if let weakSelf = self {
                if let currentvc = (weakSelf.currentVC as? RYDizhiViewController) {
                    
                    currentvc.deleteAddress(Int(weakSelf.model.id ?? "") ?? 0)
                }
            }
        }
        return v
    }()
   

    public func configureView(_ model: RYAddressListModel) {
        self.model = model
        shouhuorenLb.text = String(format: "收货人：%@ %@", model.name ?? "" , model.mobile ?? "")
        dizhiLb.text = String(format: "%@%@%@%@%@", model.provinceName ?? "" , model.cityName ?? "" , model.districtName ?? "" , model.streetName ?? "" , model.address ?? "")
        if let d = model.isDefault, d == 0 {
            morenBtn.isSelected = false
        } else {
            morenBtn.isSelected = true
        }
    }
    
    override func initViews() {
        self.backgroundColor = UIColor(hex: 0xF2F2F2)
        contentView.addSubview(bgView)
        bgView.addSubview(shouhuorenLb)
        bgView.addSubview(dizhiLb)
        bgView.addSubview(lineView)
        bgView.addSubview(self.morenBtn)
        bgView.addSubview(self.bianjiBtn)
        
        bgView.addSubview(self.shanchuBtn)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.bottom.equalToSuperview()
        }
        shouhuorenLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(17.5))
            make.left.equalToSuperview().offset(autoWidth(12.0))
            make.right.equalToSuperview().offset(autoWidth(-12.0))
            make.height.equalTo(14.0.autoWidth())
        }
        dizhiLb.snp.makeConstraints { (make) in
            make.top.equalTo(shouhuorenLb.snp.bottom).offset(autoWidth(15.0))
            make.left.equalToSuperview().offset(autoWidth(12.0))
            make.right.equalToSuperview().offset(autoWidth(-12.0))
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(dizhiLb.snp.bottom).offset(autoWidth(8.0))
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        morenBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(autoWidth(11.5))
            make.left.equalToSuperview().offset(autoWidth(12.5))
            make.height.equalTo(19.5.autoWidth())
            make.bottom.equalToSuperview().offset(autoWidth(-11.0))
        }
        bianjiBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(morenBtn)
            make.right.equalTo(shanchuBtn.snp.left).offset(autoWidth(-25.0))
            make.height.equalTo(19.5.autoWidth())
        }
        shanchuBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(morenBtn)
            make.right.equalToSuperview().offset(autoWidth(-12.5))
            make.height.equalTo(19.5.autoWidth())
        }
        
    }
}

